package com.lc.servlet;

import com.lc.bean.Commodity;
import com.lc.service.CommodityService;
import com.lc.service.impl.CommodityServiceImpl;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @description:控制层
 * @author: ManolinCoder
 * @time: 2021/8/19
 */
@WebServlet(value = "/getCommodities")
public class CommodityServlet extends HttpServlet {
    String charCode = null;
    @Override
    public void init(ServletConfig servletConfig) throws ServletException {
        charCode = servletConfig.getServletContext().getInitParameter("charCode");
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding(charCode);
        //1.接收请求参数
        //2.调取service层方法
        CommodityService commodityService = new CommodityServiceImpl();
        List<Commodity> commodities = commodityService.getAllCommodities();
        //3.跳转页面
        req.setAttribute("commodities",commodities);
        req.getRequestDispatcher("/showMessages.jsp").forward(req,resp);
    }
}
